package edu.cs335pl.questionnaire.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ServerResponse {
    //响应码
    private int code;
    //信息
    private String message;
    //数据
    private Object data;

    public ServerResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public ServerResponse(Object data){
        this.data=data;
    }

    private ServerResponse(ResponseCode responseCode){
        this.code=responseCode.getCode();
        this.message=responseCode.getMessage();
    }

    @JsonIgnore
    public boolean isSuccess() {
        //0成功，1失败
        return this.code == ResponseCode.SUCCESS.getCode();
    }

    public static ServerResponse ofSuccess() {
        return new ServerResponse(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage());
    }

    public static ServerResponse ofSuccess(String msg) {
        return new ServerResponse(ResponseCode.SUCCESS.getCode(), msg);
    }

    public static ServerResponse ofSuccess(Object obj) {
        return new ServerResponse(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), obj);
    }

    public static ServerResponse ofSuccess(String msg, Object obj) {
        return new ServerResponse(ResponseCode.SUCCESS.getCode(), msg, obj);
    }

    public static ServerResponse ofSuccess(ResponseCode responseCode){
        return new ServerResponse(responseCode.getCode(), responseCode.getMessage());
    }

    public static ServerResponse ofSuccess(int code,String msg){
        return new ServerResponse(code,msg,null);
    }

    public static ServerResponse ofSuccess(int code, String msg, Object obj) {
        return new ServerResponse(code, msg, obj);
    }

    public static ServerResponse ofError() {
        return new ServerResponse(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMessage());
    }

    public static ServerResponse ofError(String msg) {
        return new ServerResponse(ResponseCode.ERROR.getCode(), msg);
    }

    public static ServerResponse ofError(Object obj) {
        return new ServerResponse(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getMessage(), obj);
    }

    public static ServerResponse ofError(String msg, Object obj) {
        return new ServerResponse(ResponseCode.ERROR.getCode(), msg, obj);
    }

    public static ServerResponse ofError(int code,String msg){
        return new ServerResponse(code,msg,null);
    }

    public static ServerResponse ofError(ResponseCode responseCode){
        return new ServerResponse(responseCode.getCode(), responseCode.getMessage());
    }

    public static ServerResponse ofError(int code, String msg, Object obj) {
        return new ServerResponse(code, msg, obj);
    }

    public int getCode() {
        return code;
    }

    public Object getData() {
        return data;
    }

    public String getMessage() {
        return message;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "ServerResponse{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

}
